import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { getLoginUserUsingGet } from '@/servers/api/userController.ts'

export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<API.LoginUserVO>({
    userName: ""
  })

  // 获取用户信息
  const fetchLoginUser = async () => {
    const res = await getLoginUserUsingGet()
    if (res.code === 200 && res.data) {
      loginUser.value = res.data
    }
  }

  // 设置用户信息
  const setLoginUser = (user: { userName: string }) => {
    loginUser.value = user
  }

  // 清除用户信息
  const clearLoginUser = () => {
    loginUser.value = {
      userName: ""
    }
  }

  return { loginUser, fetchLoginUser, setLoginUser, clearLoginUser }
})
